using System;
using System.Reflection;
using System.Text;
using UnityEngine;

public class DebugManager : MonoBehaviour
{
    public static bool DoLog { get; set; }
    public static bool DoLogError { get; set; }
    public static bool DoLogWarning { get; set; }
    public static int CurrentPriority { get; set; }

    [AttributeUsage(AttributeTargets.Method)]
    public class CriticalDelegateAttribute : Attribute { }
    public static string GetDelegateName(Delegate del)
    {
        StringBuilder str = new StringBuilder();
        if (del.Method.Name.Contains("<"))
        {
            str.Append("Anonymous Delegate:");
        }

        if (del.Method.IsStatic)
        {
            str.Append("(Static) " + del.Method.DeclaringType?.Name + ".");
        }
        else if (del.Target as UnityEngine.Object != null)
        {
            str.Append(((UnityEngine.Object) del.Target).name + ".");
        }
        else
        {
            str.Append(del.Target?.GetType().Name + ".");
        }

        str.Append(del.Method.Name);
        if (del.Method.GetCustomAttribute(typeof(CriticalDelegateAttribute)) != null)
        {
            str.Append("(Critical)");
        }
        return str.ToString();
    }

    public static void Log(string content, int priority=0)
    {
        if (DoLog && priority >= CurrentPriority) Debug.Log(content);
    }

    public static void LogError(string content, int priority=0)
    {
        if (DoLogError && priority >= CurrentPriority) Debug.LogError(content);
    }

    public static void LogWarning(string content, int priority=0)
    {
        if (DoLogWarning && priority >= CurrentPriority) Debug.LogWarning(content);
    }
}
